package paopao
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	import utils.Arrow;
	import utils.Ball;

	/**
	 * 射击的球体
	 * @author sjp
	 *
	 */
	public class Shoot extends Sprite
	{
		//球
		private var _shootBall:Ball;
		//箭头
		private var _arrow:Arrow;
		/**
		 * 球移动的范围
		 */
		private var _rect:Rectangle;
		/**
		 * 球是否射出
		 */
		private var _isShooted:Boolean = false;

		public function Shoot()
		{
			super();
			if (stage)
			{
				init()
			}
			else
			{
				this.addEventListener(Event.ADDED_TO_STAGE, init);
			}
		}

		public function get isShooted():Boolean
		{
			return _isShooted;
		}

		public function get shootBall():Ball
		{
			return _shootBall;
		}

		public function set rect(value:Rectangle):void
		{
			_rect = value;
		}

		public function enterFrameHandler(event:Event):void
		{
			if (!_isShooted)
			{
				_shootBall.x += _shootBall.vx;
				_shootBall.y += _shootBall.vy;
				if (isTouchWalls(_shootBall))
				{
					reBorn();
				}
			}
		}

		/**
		 * 重新设置球体
		 */
		public function reBorn():void
		{
			removeChild(_shootBall);
			addShootBall();
			_isShooted = false;
		}

		private function init(event:Event = null):void
		{
			this.addEventListener(Event.ADDED_TO_STAGE, init);
			if (!_rect)
			{
				_rect = new Rectangle();
			}
			_arrow = new Arrow();
			_arrow.x = stage.stageWidth / 2;
			_arrow.y = stage.stageHeight - 10;
			addChild(_arrow);

			addShootBall();

			stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
			stage.addEventListener(MouseEvent.CLICK, mouseClickHandler);
		}

		private function addShootBall():void
		{
			_shootBall = new Ball(40, 0xFF0000);
			_shootBall.x = stage.stageWidth / 2;
			_shootBall.y = stage.stageHeight;
			addChild(_shootBall);
		}

		/**
		 * 鼠标移动监听，调整箭头的方向
		 * @param event
		 *
		 */
		private function mouseMoveHandler(event:MouseEvent):void
		{
			var dx:Number = stage.mouseX - _arrow.x;
			var dy:Number = stage.mouseY - _arrow.y;
			var radians:Number = Math.atan2(dy, dx);
			_arrow.rotation = radians * 180 / Math.PI;
		}

		/**
		 * 鼠标键点击，设置球的速度
		 * @param event
		 *
		 */
		private function mouseClickHandler(event:MouseEvent):void
		{
			if (!_isShooted)
			{
				_shootBall.mass = 2;
				var v:Number = Math.random() * 5 + 10;
				var rotation:Number = _arrow.rotation;
				var radians:Number = rotation * Math.PI / 180;
				_shootBall.vx = v * Math.cos(radians);
				_shootBall.vy = v * Math.sin(radians);
				_isShooted = true;
			}
		}

		private function isTouchWalls(ball:Ball):Boolean
		{
			var returnValue:Boolean = false;
			//最右部
			if (ball.x + ball.radius > _rect.right)
			{
				returnValue = true;
			}
			//最左部
			else if (ball.x - ball.radius < _rect.left)
			{
				returnValue = true;
			}

			//最上部
			if (ball.y - ball.radius < _rect.top)
			{
				returnValue = true;
			}
			return returnValue;
		}
	}
}